
class ArrayStack:
    '''定义栈数据结构'''
    def __init__(self):
        self._data = []

    def __len__(self):
        return len(self._data)

    def is_empty(self):
        return len(self._data) == 0

    def push(self, e):
        self._data.append(e)

    def top(self):
        if self.is_empty():
            raise ("Stack is empty")
        return self._data[-1]

    def pop(self):
        if self.is_empty():
            raise ("Stack is empty")
        return self._data.pop()

def reverse_file(filename):
    '''
    使用栈实现数据逆序
    '''
    S = ArrayStack()
    original = open(filename)
    for line in original:
        S.push(line.rstrip('\n'))
    original.close()

    output = open(filename, 'w')
    while S.is_empty():
        output.write(S.pop() + '\n')
    output.close()

def is_matched(expr):
    '''
    在算数表达式中分隔符匹配算法:
    :param expr:
    :return:
    '''
    lefty = '({['
    righty = ')}]'
    S = ArrayStack()
    for c in expr:
        if c in lefty:
            S.push(c)
        elif c in righty:
            if S.is_empty():
                return False
            if righty.index(c) != lefty.index(S.pop()):
                return False
    return S.is_empty()

# ret = is_matched('[(5+x)-(y+z)]')
# print(ret)

def is_matched_html(raw):
    S = ArrayStack()
    j = raw.find('<')
    while j != -1:
        k = raw.find('>', j+1)
        if k == -1:
            return False
        tag = raw[j+1:k]
        if not tag.startswith('/'):
            S.push(tag)
        else:
            if S.is_empty():
                return False
            if tag[1:] != S.pop():
                return False
        j = raw.find('<', k+1)
    return S.is_empty()

raw = '''
<body>
<center>
<h1> The little boat </h1>
</center>
</body>
'''

ret = is_matched_html(raw)
print(ret)